package com.qf.smsplatform.webmaster.controller;


import com.github.pagehelper.PageInfo;
import com.qf.smsplatform.webmaster.annotations.LogOperationAnno;
import com.qf.smsplatform.webmaster.dto.R;
import com.qf.smsplatform.webmaster.pojo.MyAuthUser;
import com.qf.smsplatform.webmaster.pojo.TSmsTemplate;
import com.qf.smsplatform.webmaster.service.SmsTempService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/sms")
@Api(tags = "短信模板管理")
public class SmsTempController {


    private SmsTempService smsTempService;

    @Autowired
    public void setSmsTempService(SmsTempService smsTempService) {
        this.smsTempService = smsTempService;
    }

    //添加
    @PostMapping("/sms/{mobile}")
    @ApiOperation("添加的短信模板")
    @LogOperationAnno("添加的短信模板")
    public R add(@PathVariable String mobile) {
        TSmsTemplate tSmsTemplate = new TSmsTemplate();
        tSmsTemplate.setTemplate(mobile);

        MyAuthUser user = (MyAuthUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user != null) {
            tSmsTemplate.setOwntype(user.getType());
            tSmsTemplate.setCreater(user.getId());
        }
        smsTempService.add(tSmsTemplate);
        return R.setOk();
    }

    //查询所有
    @GetMapping("/showAll")
    @ApiOperation("查询所有短信模板")
    @LogOperationAnno("查询所有短信模板")
    public R findAll(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        PageInfo<TSmsTemplate> all = smsTempService.findAll(pageNum, pageSize);
        return R.setOk(all);
    }

    //修改
    @PostMapping("/sms")
    @ApiOperation("修改的短信模板")
    @LogOperationAnno("修改短信模板")
    public R updateBlackById(@RequestBody TSmsTemplate tSmsTemplate) {
        MyAuthUser user = (MyAuthUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user != null) {
            Long id = user.getId();
            Long type = user.getType();
            tSmsTemplate.setCreater(id);
            tSmsTemplate.setOwntype(type);
        }
        smsTempService.update(tSmsTemplate);
        return R.setOk();
    }

    //删除
    @DeleteMapping("/sms")
    @ApiOperation("删除黑名单")
    @LogOperationAnno("删除黑名单")
    public R deleteBlackById(@RequestBody Map map) {
        smsTempService.delete((List<Integer>) map.get("ids"));
        return R.setOk();
    }


    @GetMapping("/tsms")
    @ApiOperation("动态查询模板")
    @LogOperationAnno("动态查询短信模板")
    public R DynamicQuery(TSmsTemplate tSmsTemplate) {
        List<TSmsTemplate> tSmsTemplates = smsTempService.DynamicQuery(tSmsTemplate);
        return R.setOk(tSmsTemplates);
    }
}
